package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.topboya.linker.R;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.activity.AddCustomerActivity;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by aishua on 2018/4/3.
 */

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHoler> {
    private static final String TAG = "CustomerAdapter:";
    private Context mContext;
    private BaseActivity mActivity;
    private ArrayList<OrganizationResopnse.DocDetailInfoList> groupList;
    private HashMap<String, List<CustomerByOrgIdResponse.DepartmentList>> departmentgroupmap;

    public CustomerAdapter(Context mContext, ArrayList<OrganizationResopnse.DocDetailInfoList> groupList, HashMap<String, List<CustomerByOrgIdResponse.DepartmentList>> departmentgroupmap) {
        this.mContext = mContext;
        this.mActivity = (BaseActivity) mContext;
        this.groupList =  groupList;
        this.departmentgroupmap = departmentgroupmap;
    }

    @Override
    public CustomerViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.expandablelist_group_customer, parent, false);

        return new CustomerViewHoler(view);

    }

    @Override
    public void onBindViewHolder(CustomerViewHoler holder, final int position) {

        holder.displayView(position);
        holder.itemView.setOnClickListener(v -> onRecycleViewClickListener.onRecycleViewClick(position));

    }

    @Override
    public int getItemCount() {

        if (null == groupList || groupList.size() == 0) {

            return 0;
        }
        return groupList.size();

//        return  3;
    }

    public class CustomerViewHoler extends RecyclerView.ViewHolder {

        private TextView tv_hasptial_level;
        private TextView tv_hasptial_name;
        private LinearLayout ll_doc_detail;
        private LinearLayout ll_department_detail;
        private GridView gv_dapartment;
        private View itemView;
        private TextView tv_department;
        private ImageView img_add_doctor;

        public CustomerViewHoler(View itemView) {

            super(itemView);
            this.itemView = itemView;
            tv_hasptial_name = itemView.findViewById(R.id.tv_hasptial_name);
            ll_doc_detail = itemView.findViewById(R.id.ll_doc_detail);
            ll_department_detail = itemView.findViewById(R.id.ll_department_detail);
            img_add_doctor = itemView.findViewById(R.id.img_add_doctor);
        }

        public void displayView(int position) {

            LogUtils.v("displayView()");
            String orgName = groupList.get(position).getOrgName();
            tv_hasptial_name.setText(orgName);
            img_add_doctor.setOnClickListener(v -> {

                Intent intent = new Intent(mContext,AddCustomerActivity.class);
                intent.putExtra("DocDetailInfoList", groupList.get(position));
                mContext.startActivity(intent);

            });


            if (null != departmentgroupmap) {
                ViewGroup.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

                LogUtils.v(TAG + "getAdapterPosition():"+getAdapterPosition());
                if (getAdapterPosition() ==position ){
                    for (Map.Entry<String, List<CustomerByOrgIdResponse.DepartmentList>> entry : departmentgroupmap.entrySet()) {
                        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                        tv_department = new TextView(mContext);
                        gv_dapartment = new GridView(mContext);
                        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,(entry.getValue().size() / 3 )+1 *100);
                        MyGridViewAdapter gridViewAdapter = new MyGridViewAdapter(mContext,entry.getValue());
                        gv_dapartment.setLayoutParams(layoutParams);
                        gv_dapartment.setNumColumns(3);
                        gv_dapartment.setAdapter(gridViewAdapter);
                        tv_department.setText(entry.getKey());
                        tv_department.setBackgroundColor(Color.BLUE);
                        tv_department.setLayoutParams(layoutParams1);
                        ll_doc_detail.addView(tv_department);
                        ll_department_detail.addView(gv_dapartment);

                    }
                }
            }
        }


    }

    public void setonRecycleViewClickListener(onRecycleViewClickListener onRccycleViewLongClickListener) {
        this.onRecycleViewClickListener = onRccycleViewLongClickListener;
    }

    public void setOnRccycleViewLongClickListener(onRccycleViewLongClickListener onRccycleViewLongClickListener) {

        this.onRccycleViewLongClickListener = onRccycleViewLongClickListener;
    }

    private onRecycleViewClickListener onRecycleViewClickListener;
    private onRccycleViewLongClickListener onRccycleViewLongClickListener;

    public interface onRecycleViewClickListener {

        public void onRecycleViewClick(int position);
    }

    public interface onRccycleViewLongClickListener {

        public void onRccycleViewLongClick();
    }

    //    刷新item
    public void refreshItem(ArrayList<OrganizationResopnse.DocDetailInfoList> groupList, HashMap<String, List<CustomerByOrgIdResponse.DepartmentList>> departmentgroupmap,int position) {

        this.groupList = groupList;
        this.departmentgroupmap = departmentgroupmap;
        notifyItemChanged(position);
    }

}